package com.map.domain;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ContentWriter {

	private List<String> something;
	private String content;
	private List<String> urlList;
	private List<String> timeList;
	private List<String>latlon;

	private String filename;
	private String filepath;
	
	public ContentWriter()
	{
		content=null;
		urlList=new ArrayList<String>();
		timeList=new ArrayList<String>();
		latlon= new ArrayList<String>();
		something=new ArrayList<String>();
		
	}
	//getter and setters must be specified inorder to be able to achieve the mapping
	public String getContent()
	{
		return this.content;
	}
	public void setContent(String content)
	{
		this.content=content;
	}
	
	public List<String> geturlList()
	{
		return this.urlList;
	}
	public void seturlList(List<String> urlList)
	{
		this.urlList=urlList;
	}
	
	

	
	
	public List<String> getlatlon()
	{
		return this.latlon;
	}
	
	
	public void setlatlon(List<String> l)
	{
		this.latlon=l;
	}
	
	
	public List<String> getsomething()
	{
		return this.something;
	}
	
	
	public void setsomething(List<String> l)
	{
		this.something=l;
	}
	
	
	public List<String> gettimeList()
	{
		return this.timeList;
	}
	public void settimeList(List<String> timeList)
	{
		this.timeList=timeList;
	}
	
	public String getFilename()
	{
		return this.filename;
	}
	public void setFilename(String filename)
	{
		this.filename=filename;
	}
	
	public String getFilepath()
	{
		return this.filepath;
	}
	
	public void setFilepath(String filepath)
	{
		this.filepath=filepath;
	}
	
	
	public void writeListtoFile() 
	{
		try{
		System.out.println("Inside writeListtoFile");
		File newFile = new File(filepath + filename);  
		   if (!newFile.exists()) {
			   System.out.println("The file does not exist, need creation");
		    newFile.createNewFile();  
		   }
		   FileWriter fw = new FileWriter(newFile.getAbsoluteFile(), true);
		for(int i=0;i<urlList.size();i++)
		{
			System.out.print(urlList.get(i)+"\t\t\t"+timeList.get(i)+"\t\t\t"+latlon.get(i)+"\t\t\t"+something.get(i)+"\n");
			fw.write(urlList.get(i)+"\t\t\t"+timeList.get(i)+"\t\t\t"+latlon.get(i)+"\t\t\t"+something.get(i)+"\n");
		}
		fw.close();
		System.out.println("In total, "+urlList.size()+" number of records written");
		}
		catch (IOException e){
			System.out.println("There is an io exption when writing to Result.txt"+e.getMessage());
		}
	}
	public static List<String> readHistory(String pathname, String filename)
	{
		List<String> webList = new LinkedList<String>();
		File newFile = new File(pathname + filename);  
		   if (!newFile.exists()) {
			   System.out.println("The file does not exist, return empty list");
		     return webList;  
		   }
		   String line=null;
		  try {
			BufferedReader br=  new BufferedReader(new FileReader(newFile));
		while((line=br.readLine())!=null)
		{
			System.out.println(line);
			if(!line.equals(""))
			webList.add(line);
			else
				System.out.println("Meet empty line");
		}
		
		br.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		  //System.out.println("Readed size: "+ webList.size());
	return webList;
	}
	
	public void writeContent()
	{
		try{
		File newFile = new File("D:/GeoFiles/" + "Result.html");  
		   if (!newFile.exists()) {
			   System.out.println("The file does not exist, need creation");
		    newFile.createNewFile();  
		   }
		   FileWriter fw = new FileWriter(newFile.getAbsoluteFile(), true);
		   fw.write(content+"\n");
		   fw.close();
		}
		catch (IOException e){
			System.out.println("There is an io exption when writing to Result.html"+e.getMessage());
		}
	}
}
